<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\PeizhenService;
use Illuminate\Http\Request;

class PeizhenController extends BaseController
{
    //陪诊

    public function __construct(PeizhenService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getList(Request $request)
    {
        $locate = $request->post('locate', '');
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $res = $this->db->getList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //列表
    public function getOne(Request $request)
    {
        $id = $request->post('id', '');
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //发布
    public function add(Request $request)
    {
        $token = $request->post('token', '');
        $pas['pat_uid'] = $this->db->uidByToken($token);
        if (!$pas['pat_uid']) {
            return $this->toJson(-2, '没有登录');
        }
        $pas['title'] = $request->post('title', ''); //订单标题
        $pas['pat_name'] = $request->post('pat_name', ''); //患者名称
        $pas['pat_phone'] = $request->post('pat_phone', ''); //患者手机号
        $pas['start_time'] = $request->post('start_time', ''); //服务开始时间
        $pas['end_time'] = $request->post('end_time', ''); //服务结束时间
        $pas['yiyuan'] = $request->post('yiyuan', ''); //医院名称
        $pas['delay'] = $request->post('delay', 0); //时长多少小时
        $pas['price'] = $request->post('price', 0); //费用
        $pas['yy_map'] = $request->post('yy_map', ''); //预约时经纬度
        $pas['remark'] = $request->post('remark', '');
        if (!$pas['title']) {
            return $this->toJson(-2, '标题必须的');
        }
        if (!$pas['pat_name'] || !$pas['pat_phone']) {
            return $this->toJson(-2, '病人姓名、手机号必须的');
        }
        if (!$pas['delay'] || !$pas['price']) {
            return $this->toJson(-2, '服务时长、费用必须的');
        }
        if (!$pas['yiyuan']) {
            return $this->toJson(-2, '医院必须的');
        }
        $res = $this->db->add($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-1, $res['msg']);
        }
        return $res;
    }

    /**
     * 医生接单
     */
    public function rob(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $res = $this->db->rob($id, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 服务取消
     */
    public function close(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $close_remark = $request->post('remark', '');
        $res = $this->db->close($id, $close_remark);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 服务开始
     */
    public function keep(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $res = $this->db->keep($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 服务完成
     */
    public function finish(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $res = $this->db->finish($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 陪诊价格
     * 按时长计算
     */
    public function price(Request $request)
    {
        $start_time = $request->post('start_time', '');
        $end_time = $request->post('end_time', '');
        if (!$start_time || !$end_time) {
            return $this->toJson(-2, '开始、结束时间必须的');
        }
        $start = strtotime($start_time);
        $end = strtotime($end_time);
        if ($start > $end) {
            return $this->toJson(-2, '开始时间不能大于结束时间');
        }
        $delay = $end - $start;
        if ($delay <= 60) {
            return $this->toJson(-2, '时长必须大于1分钟');
        }
        //计算时长
        $hour = intval($delay / 3600);
        $res = $this->db->price($hour);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}
